package com.cx.service.impl;

import com.cx.mapper.GoodCollectionMapper;
import com.cx.pojo.GoodCollection;
import com.cx.pojo.GoodCollectionExample;
import com.cx.service.CollectService;
import com.cx.vo.GoodCollectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Zhou wei
 * @create: 2020/5/19 20:36
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    GoodCollectionMapper goodCollectionMapper;

    @Override
    public int addCollect(Integer userId, Integer goodId) {
        GoodCollection goodCollection = new GoodCollection();
        goodCollection.setUserId(userId);
        goodCollection.setGoodId(goodId);
        goodCollection.setCreateTime(new Date());
        goodCollection.setUpdateTime(new Date());

        int affectedRows = goodCollectionMapper.insertSelective(goodCollection);
        return affectedRows;
    }

    @Override
    public int deleteCollect(Integer userId, Integer goodId) {
        GoodCollectionExample goodCollectionExample = new GoodCollectionExample();
        GoodCollectionExample.Criteria criteria = goodCollectionExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andGoodIdEqualTo(goodId);

        int affectedRows = goodCollectionMapper.deleteByExample(goodCollectionExample);
        return affectedRows;
    }

    @Override
    public List<GoodCollection> selectByUserIdAndGoodId(Integer userId, Integer goodId) {
        GoodCollectionExample goodCollectionExample = new GoodCollectionExample();
        GoodCollectionExample.Criteria criteria = goodCollectionExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andGoodIdEqualTo(goodId);

        List<GoodCollection> goodCollectionList = goodCollectionMapper.selectByExample(goodCollectionExample);
        return goodCollectionList;
    }

    @Override
    public List<GoodCollectVO> selectFromGoods(Integer userId) {
        List<GoodCollectVO> goodCollectVOList = goodCollectionMapper.selectFromGoods(userId);
        return goodCollectVOList;
    }

    @Override
    public List<GoodCollectVO> selectByGoodName(Integer userId,String goodName) {
        List<GoodCollectVO> goodCollectVOList = goodCollectionMapper.selectByGoodName(userId,goodName);
        return goodCollectVOList;
    }
}
